<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\ServerResponse;
use app\model\Hot_update;
use app\model\Waytype;
use think\Request;

class BlogType
{
    //获取当前栏目数据
    public function getCurrentType(Request $request){
        $tid = $request->post('tid');
        $data=\app\model\Blogtype::where('tid',$tid)->find();
        if ($data) {
            return ServerResponse::jsonData(1, '获取当前栏目详情成功!', $data);
        } else {
            return ServerResponse::jsonData(0, '获取当前栏目详情失败!', $data);
        }
    }
    //获取栏目列表数据
    public function getBlogtypeList(Request $request)
    {
        $keyword = $request->post('keyword') ;
        if (trim($keyword)!='') {
            $data = \app\model\Blogtype::where('typename','like','%'.$keyword.'%')->select();
            if ($data) {
                return ServerResponse::jsonData(1, '获取分类列表成功!', $data);
            } else {
                return ServerResponse::jsonData(0, '获取失败!', null);
            }
        } else {
            $blogType = new \app\model\Blogtype();
            $data = $blogType->categoryList();
            if ($data) {
                return ServerResponse::jsonData(1, '获取分类列表成功!', $data);
            } else {
                return ServerResponse::jsonData(0, '获取失败!', null);
            }
        }
    }

    //删除栏目
    public function delBlogType(Request $request)
    {
        $tid = $request->post('tid');
        $tidData = \app\model\Blogtype::where('tid', '=', $tid)->find();
        $arrs = \app\model\Blogtype::select();
        $BlogType = new \app\model\Blogtype();
        if ($tidData['sid'] != 0) {
            $tidList = $tid;
        } else {
            $tidList = $BlogType->get_all_child($arrs, (int)$tid);
            $tidList[] = $tid;
        }
        $data = \app\model\Blogtype::destroy($tidList);
        $hot=Hot_update::hotUpdate(1);
        if ($data&&$hot) {
            return ServerResponse::jsonData(1, '删除成功!', $data);
        } else {
            return ServerResponse::jsonData(0, '删除失败!', $data);
        }

    }

    //获取发布类型
    public function getPublicType()
    {
        $data = Waytype::select();
        if ($data) {
            return ServerResponse::jsonData(1, '获取发布类型成功!', $data);
        } else {
            return ServerResponse::jsonData(0, '获取失败!', $data);
        }
    }
    //新增+更新栏目
    public function insertNewType(Request $request){
        $tid=$request->post('tid');
        $title = $request->post('title');
        $sid=$request->post('sid');
        if(isset($tid)){
            $BlogType=\app\model\Blogtype::where('tid','=',$tid)->find(1);
            $condition=['typename'=>$title,'sid'=>$sid];
            $data=$BlogType->save($condition);
            if ($data) {
                return ServerResponse::jsonData(1, '更新成功!', null);
            } else {
                return ServerResponse::jsonData(0, '更新失败!', $data);
            }
        }else{
            $data=\app\model\Blogtype::create([
                'typename'=>$title,
                'sid'=>$sid
            ]);
            $hot=Hot_update::hotUpdate(1);
            if ($data&&$hot) {
                return ServerResponse::jsonData(1, '添加成功!', null);
            } else {
                return ServerResponse::jsonData(0, '添加失败!', $data);
            }
        }
    }
}
